// MainWnd.cpp : implementation file
//

#include "stdafx.h"
#include <winsock.h>

#include "resource.h"
#include "MainWnd.h"

#include "CmdAccelOb.h"
#include "Reg.h"
#include "WinResUtil.h"
#include "WinMiscUtil.h"
#include "Input.h"
#include "Sound.h"
#include "VBA.h"
#include "7zip/7zip.h"
#include "7zip/OpenArchive.h"
#include "Dialogs/FileDlg.h"
#include "Dialogs/ModeConfirm.h"
#include "Dialogs/LuaOpenDialog.h"
#include "Dialogs/ram_search.h"
#include "Dialogs/ramwatch.h"

#include "../version.h"
#include "../common/Util.h"
#include "../common/movie.h"
#include "../common/vbalua.h"
#include "../common/System.h"
#include "../common/SystemGlobals.h"
#include "../gba/GBAGlobals.h"
#include "../gb/gbGlobals.h"

#define VBA_CONFIRM_MODE WM_APP + 100

/////////////////////////////////////////////////////////////////////////////
// MainWnd

MainWnd::MainWnd()
{
	m_hAccelTable = NULL;
	arrow		  = LoadCursor(NULL, IDC_ARROW);

	InitDecoder();
}

MainWnd::~MainWnd()
{
	CleanupDecoder();
}

BEGIN_MESSAGE_MAP(MainWnd, CWnd)
//{{AFX_MSG_MAP(MainWnd)
ON_WM_MOVE()
ON_WM_SIZE()
ON_WM_CLOSE()
ON_WM_INITMENUPOPUP()
ON_WM_INITMENU()
ON_WM_CONTEXTMENU()
ON_WM_MOUSEMOVE()
ON_WM_ACTIVATE()
ON_WM_ACTIVATEAPP()
ON_WM_DROPFILES()
ON_WM_PAINT()

ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
ON_COMMAND(ID_HELP_FAQ, OnHelpFaq)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_OPENGAMEBOY, OnFileOpenGBx)
ON_COMMAND(ID_FILE_PAUSE, OnFilePause)
ON_UPDATE_COMMAND_UI(ID_FILE_PAUSE, OnUpdateFilePause)
ON_COMMAND(ID_FILE_RESET, OnFileReset)
ON_UPDATE_COMMAND_UI(ID_FILE_RESET, OnUpdateFileReset)
ON_UPDATE_COMMAND_UI(ID_FILE_RECENT_FREEZE, OnUpdateFileRecentFreeze)
ON_COMMAND(ID_FILE_RECENT_RESET, OnFileRecentReset)
ON_COMMAND(ID_FILE_RECENT_FREEZE, OnFileRecentFreeze)
ON_COMMAND(ID_FILE_EXIT, OnFileExit)
ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
ON_UPDATE_COMMAND_UI(ID_FILE_CLOSE, OnUpdateFileClose)
ON_COMMAND(ID_FILE_LOAD, OnFileLoad)
ON_UPDATE_COMMAND_UI(ID_FILE_LOAD, OnUpdateFileLoad)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
ON_COMMAND(ID_FILE_IMPORT_BATTERYFILE, OnFileImportBatteryfile)
ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_BATTERYFILE, OnUpdateFileImportBatteryfile)
ON_COMMAND(ID_FILE_IMPORT_GAMESHARKCODEFILE, OnFileImportGamesharkcodefile)
ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_GAMESHARKCODEFILE, OnUpdateFileImportGamesharkcodefile)
ON_COMMAND(ID_FILE_IMPORT_GAMESHARKSNAPSHOT, OnFileImportGamesharksnapshot)
ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_GAMESHARKSNAPSHOT, OnUpdateFileImportGamesharksnapshot)
ON_COMMAND(ID_FILE_EXPORT_BATTERYFILE, OnFileExportBatteryfile)
ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT_BATTERYFILE, OnUpdateFileExportBatteryfile)
ON_COMMAND(ID_FILE_EXPORT_GAMESHARKSNAPSHOT, OnFileExportGamesharksnapshot)
ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT_GAMESHARKSNAPSHOT, OnUpdateFileExportGamesharksnapshot)
ON_COMMAND(ID_FILE_QUICKSCREENCAPTURE, OnFileQuickScreencapture)
ON_COMMAND(ID_FILE_SCREENCAPTURE, OnFileScreencapture)
ON_UPDATE_COMMAND_UI(ID_FILE_SCREENCAPTURE, OnUpdateFileScreencapture)
ON_COMMAND(ID_FILE_ROMINFORMATION, OnFileRominformation)
ON_UPDATE_COMMAND_UI(ID_FILE_ROMINFORMATION, OnUpdateFileRominformation)
ON_COMMAND(ID_FILE_TOGGLEMENU, OnFileTogglemenu)
ON_UPDATE_COMMAND_UI(ID_FILE_TOGGLEMENU, OnUpdateFileTogglemenu)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_6, OnUpdateOptionsFrameskipThrottle6)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_15, OnUpdateOptionsFrameskipThrottle15)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_25, OnUpdateOptionsFrameskipThrottle25)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_50, OnUpdateOptionsFrameskipThrottle50)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_75, OnUpdateOptionsFrameskipThrottle75)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_100, OnUpdateOptionsFrameskipThrottle100)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_125, OnUpdateOptionsFrameskipThrottle125)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_150, OnUpdateOptionsFrameskipThrottle150)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_200, OnUpdateOptionsFrameskipThrottle200)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_300, OnUpdateOptionsFrameskipThrottle300)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_400, OnUpdateOptionsFrameskipThrottle400)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_600, OnUpdateOptionsFrameskipThrottle600)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_1000, OnUpdateOptionsFrameskipThrottle1000)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER, OnUpdateOptionsFrameskipThrottleOther)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_INCREASE, OnUpdateOptionsFrameskipThrottleIncrease)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_DECREASE, OnUpdateOptionsFrameskipThrottleDecrease)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_6, OnOptionsFrameskipThrottle6)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_15, OnOptionsFrameskipThrottle15)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_25, OnOptionsFrameskipThrottle25)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_50, OnOptionsFrameskipThrottle50)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_75, OnOptionsFrameskipThrottle75)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_100, OnOptionsFrameskipThrottle100)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_125, OnOptionsFrameskipThrottle125)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_150, OnOptionsFrameskipThrottle150)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_200, OnOptionsFrameskipThrottle200)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_300, OnOptionsFrameskipThrottle300)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_400, OnOptionsFrameskipThrottle400)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_600, OnOptionsFrameskipThrottle600)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_1000, OnOptionsFrameskipThrottle1000)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER, OnOptionsFrameskipThrottleOther)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_INCREASE, OnOptionsFrameskipThrottleIncrease)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_DECREASE, OnOptionsFrameskipThrottleDecrease)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_ACCURATEPITCH, OnOptionsFrameskipAccuratePitch)
ON_COMMAND(ID_OPTIONS_FRAMESKIP_ACCURATESPEED, OnOptionsFrameskipAccurateSpeed)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_ACCURATEPITCH, OnUpdateOptionsFrameskipAccuratePitch)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_ACCURATESPEED, OnUpdateOptionsFrameskipAccurateSpeed)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_0, OnUpdateOptionsVideoFrameskip0)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_1, OnUpdateOptionsVideoFrameskip1)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_2, OnUpdateOptionsVideoFrameskip2)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_3, OnUpdateOptionsVideoFrameskip3)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_4, OnUpdateOptionsVideoFrameskip4)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_5, OnUpdateOptionsVideoFrameskip5)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_6, OnUpdateOptionsVideoFrameskip6)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_7, OnUpdateOptionsVideoFrameskip7)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_8, OnUpdateOptionsVideoFrameskip8)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_9, OnUpdateOptionsVideoFrameskip9)
ON_COMMAND(ID_OPTIONS_VIDEO_VSYNC, OnOptionsVideoVsync)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_VSYNC, OnUpdateOptionsVideoVsync)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X1, OnUpdateOptionsVideoX1)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X2, OnUpdateOptionsVideoX2)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X3, OnUpdateOptionsVideoX3)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X4, OnUpdateOptionsVideoX4)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN320X240, OnUpdateOptionsVideoFullscreen320x240)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN640X480, OnUpdateOptionsVideoFullscreen640x480)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN800X600, OnUpdateOptionsVideoFullscreen800x600)
ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN320X240, OnOptionsVideoFullscreen320x240)
ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN640X480, OnOptionsVideoFullscreen640x480)
ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN800X600, OnOptionsVideoFullscreen800x600)
ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN, OnOptionsVideoFullscreen)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN, OnUpdateOptionsVideoFullscreen)
ON_COMMAND(ID_OPTIONS_VIDEO_DISABLESFX, OnOptionsVideoDisablesfx)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DISABLESFX, OnUpdateOptionsVideoDisablesfx)
ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT, OnOptionsVideoFullscreenstretchtofit)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT, OnUpdateOptionsVideoFullscreenstretchtofit)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_GDI, OnOptionsVideoRendermethodGdi)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_GDI, OnUpdateOptionsVideoRendermethodGdi)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW, OnOptionsVideoRendermethodDirectdraw)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW, OnUpdateOptionsVideoRendermethodDirectdraw)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D, OnOptionsVideoRendermethodDirect3d)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D, OnUpdateOptionsVideoRendermethodDirect3d)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL, OnOptionsVideoRendermethodOpengl)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL, OnUpdateOptionsVideoRendermethodOpengl)
ON_COMMAND(ID_OPTIONS_VIDEO_TRIPLEBUFFERING, OnOptionsVideoTriplebuffering)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_TRIPLEBUFFERING, OnUpdateOptionsVideoTriplebuffering)
ON_COMMAND(ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY, OnOptionsVideoDdrawemulationonly)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY, OnUpdateOptionsVideoDdrawemulationonly)
ON_COMMAND(ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY, OnOptionsVideoDdrawusevideomemory)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY, OnUpdateOptionsVideoDdrawusevideomemory)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER, OnOptionsVideoRenderoptionsD3dnofilter)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER, OnUpdateOptionsVideoRenderoptionsD3dnofilter)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR, OnOptionsVideoRenderoptionsD3dbilinear)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR, OnUpdateOptionsVideoRenderoptionsD3dbilinear)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST, OnOptionsVideoRenderoptionsGlnearest)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST, OnUpdateOptionsVideoRenderoptionsGlnearest)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR, OnOptionsVideoRenderoptionsGlbilinear)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR, OnUpdateOptionsVideoRenderoptionsGlbilinear)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE, OnOptionsVideoRenderoptionsGltriangle)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE, OnUpdateOptionsVideoRenderoptionsGltriangle)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS, OnOptionsVideoRenderoptionsGlquads)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS, OnUpdateOptionsVideoRenderoptionsGlquads)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_SELECTSKIN, OnOptionsVideoRenderoptionsSelectskin)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_SELECTSKIN, OnUpdateOptionsVideoRenderoptionsSelectskin)
ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_SKIN, OnOptionsVideoRenderoptionsSkin)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_SKIN, OnUpdateOptionsVideoRenderoptionsSkin)
ON_COMMAND(ID_OPTIONS_EMULATOR_ASSOCIATE, OnOptionsEmulatorAssociate)
ON_COMMAND(ID_OPTIONS_EMULATOR_DIRECTORIES, OnOptionsEmulatorDirectories)
ON_COMMAND_RANGE(ID_OPTIONS_PREFER_ARCHIVE_NAME, ID_OPTIONS_PREFER_ROM_NAME, OnOptionsEmulatorFilenamePreference)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_PREFER_ARCHIVE_NAME, ID_OPTIONS_PREFER_ROM_NAME, OnUpdateOptionsEmulatorFilenamePreference)
ON_COMMAND(ID_OPTIONS_VIDEO_DISABLESTATUSMESSAGES, OnOptionsVideoDisablestatusmessages)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DISABLESTATUSMESSAGES, OnUpdateOptionsVideoDisablestatusmessages)
ON_COMMAND(ID_OPTIONS_EMULATOR_SYNCHRONIZE, OnOptionsEmulatorSynchronize)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SYNCHRONIZE, OnUpdateOptionsEmulatorSynchronize)

ON_COMMAND(ID_OPTIONS_EMULATOR_ALWAYSONTOP, OnOptionsEmulatorAlwaysOnTop)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_ALWAYSONTOP, OnUpdateOptionsEmulatorAlwaysOnTop)
ON_COMMAND(ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE, OnOptionsEmulatorPausewheninactive)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE, OnUpdateOptionsEmulatorPausewheninactive)
ON_COMMAND(ID_OPTIONS_EMULATOR_BACKGROUNDINPUT, OnOptionsEmulatorEnableBackgroundInput)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_BACKGROUNDINPUT, OnUpdateOptionsEmulatorEnableBackgroundInput)
ON_COMMAND(ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE, OnOptionsEmulatorSpeeduptoggle)

ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE, OnUpdateOptionsEmulatorSpeeduptoggle)
ON_COMMAND(ID_OPTIONS_EMULATOR_REMOVEINTROSGBA, OnOptionsEmulatorRemoveintrosgba)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_REMOVEINTROSGBA, OnUpdateOptionsEmulatorRemoveintrosgba)
ON_COMMAND(ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH, OnOptionsEmulatorAutomaticallyipspatch)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH, OnUpdateOptionsEmulatorAutomaticallyipspatch)
ON_COMMAND(ID_OPTIONS_EMULATOR_AGBPRINT, OnOptionsEmulatorAgbprint)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AGBPRINT, OnUpdateOptionsEmulatorAgbprint)
ON_COMMAND(ID_OPTIONS_EMULATOR_REALTIMECLOCK, OnOptionsEmulatorRealtimeclock)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_REALTIMECLOCK, OnUpdateOptionsEmulatorRealtimeclock)
ON_COMMAND(ID_OPTIONS_EMULATOR_AUTOHIDEMENU, OnOptionsEmulatorAutohidemenu)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AUTOHIDEMENU, OnUpdateOptionsEmulatorAutohidemenu)
ON_COMMAND(ID_OPTIONS_EMULATOR_REWINDINTERVAL, OnOptionsEmulatorRewindinterval)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC, OnOptionsEmulatorSavetypeAutomatic)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC, OnUpdateOptionsEmulatorSavetypeAutomatic)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM, OnOptionsEmulatorSavetypeEeprom)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM, OnUpdateOptionsEmulatorSavetypeEeprom)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_SRAM, OnOptionsEmulatorSavetypeSram)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_SRAM, OnUpdateOptionsEmulatorSavetypeSram)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH, OnOptionsEmulatorSavetypeFlash)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH, OnUpdateOptionsEmulatorSavetypeFlash)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR, OnOptionsEmulatorSavetypeEepromsensor)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR, OnUpdateOptionsEmulatorSavetypeEepromsensor)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_NONE, OnOptionsEmulatorSavetypeNone)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_NONE, OnUpdateOptionsEmulatorSavetypeNone)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K, OnOptionsEmulatorSavetypeFlash512k)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K, OnUpdateOptionsEmulatorSavetypeFlash512k)
ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M, OnOptionsEmulatorSavetypeFlash1m)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M, OnUpdateOptionsEmulatorSavetypeFlash1m)
ON_COMMAND(ID_OPTIONS_EMULATOR_USEBIOSFILE, OnOptionsEmulatorUsebiosfile)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_USEBIOSFILE, OnUpdateOptionsEmulatorUsebiosfile)
ON_COMMAND(ID_OPTIONS_EMULATOR_SKIPBIOS, OnOptionsEmulatorSkipbios)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SKIPBIOS, OnUpdateOptionsEmulatorSkipbios)
ON_COMMAND(ID_OPTIONS_EMULATOR_SELECTBIOSFILE, OnOptionsEmulatorSelectbiosfile)

#ifdef USE_GBA_CORE_V7
ON_COMMAND(ID_EMULATOR_GBALAG, OnOptionsEmulatorGBALag)
ON_UPDATE_COMMAND_UI(ID_EMULATOR_GBALAG, OnUpdateOptionsEmulatorGBALag)
#endif
ON_COMMAND(ID_EMULATOR_USE_OLD_FRAME_TIMING, OnOptionsEmulatorUseOldGBTiming)
ON_UPDATE_COMMAND_UI(ID_EMULATOR_USE_OLD_FRAME_TIMING, OnUpdateOptionsEmulatorUseOldGBTiming)
#ifdef USE_GB_CORE_V7
ON_COMMAND(ID_EMULATOR_USE_GB_INPUT_HACK, OnOptionsEmulatorUseGBNullInputHack)
ON_UPDATE_COMMAND_UI(ID_EMULATOR_USE_GB_INPUT_HACK, OnUpdateOptionsEmulatorUseGBNullInputHack)
#else
ON_COMMAND(ID_EMULATION_V20_GB_FRAME_HACK, OnOptionsEmulatorUseV20GBFrameHack)
ON_UPDATE_COMMAND_UI(ID_EMULATION_V20_GB_FRAME_HACK, OnUpdateOptionsEmulatorUseV20GBFrameHack)
#endif

ON_COMMAND(ID_OPTIONS_EMULATOR_PNGFORMAT, OnOptionsEmulatorPngformat)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_PNGFORMAT, OnUpdateOptionsEmulatorPngformat)
ON_COMMAND(ID_OPTIONS_EMULATOR_BMPFORMAT, OnOptionsEmulatorBmpformat)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_BMPFORMAT, OnUpdateOptionsEmulatorBmpformat)
ON_COMMAND(ID_OPTIONS_SOUND_DISABLE, OnOptionsSoundDisable)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DISABLE, OnUpdateOptionsSoundDisable)
ON_COMMAND(ID_OPTIONS_SOUND_MUTE, OnOptionsSoundMute)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_MUTE, OnUpdateOptionsSoundMute)
ON_COMMAND(ID_OPTIONS_SOUND_OFF, OnOptionsSoundOff)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_OFF, OnUpdateOptionsSoundOff)
ON_COMMAND(ID_OPTIONS_SOUND_ON, OnOptionsSoundOn)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_ON, OnUpdateOptionsSoundOn)
ON_COMMAND(ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION, OnOptionsSoundUseoldsynchronization)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION, OnUpdateOptionsSoundUseoldsynchronization)
ON_COMMAND(ID_OPTIONS_SOUND_ECHO, OnOptionsSoundEcho)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_ECHO, OnUpdateOptionsSoundEcho)
ON_COMMAND(ID_OPTIONS_SOUND_LOWPASSFILTER, OnOptionsSoundLowpassfilter)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_LOWPASSFILTER, OnUpdateOptionsSoundLowpassfilter)
ON_COMMAND(ID_OPTIONS_SOUND_REVERSESTEREO, OnOptionsSoundReversestereo)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_REVERSESTEREO, OnUpdateOptionsSoundReversestereo)
ON_COMMAND(ID_OPTIONS_SOUND_MUTEFRAMEADVANCE, OnOptionsSoundMuteFrameAdvance)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_MUTEFRAMEADVANCE, OnUpdateOptionsSoundMuteFrameAdvance)
ON_COMMAND(ID_OPTIONS_SOUND_MUTEWHENINACTIVE, OnOptionsSoundMuteWhenInactive)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_MUTEWHENINACTIVE, OnUpdateOptionsSoundMuteWhenInactive)
ON_COMMAND(ID_OPTIONS_SOUND_11KHZ, OnOptionsSound11khz)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_11KHZ, OnUpdateOptionsSound11khz)
ON_COMMAND(ID_OPTIONS_SOUND_22KHZ, OnOptionsSound22khz)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_22KHZ, OnUpdateOptionsSound22khz)
ON_COMMAND(ID_OPTIONS_SOUND_44KHZ, OnOptionsSound44khz)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_44KHZ, OnUpdateOptionsSound44khz)
ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL1, OnOptionsSoundChannel1)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL1, OnUpdateOptionsSoundChannel1)
ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL2, OnOptionsSoundChannel2)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL2, OnUpdateOptionsSoundChannel2)
ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL3, OnOptionsSoundChannel3)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL3, OnUpdateOptionsSoundChannel3)
ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL4, OnOptionsSoundChannel4)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL4, OnUpdateOptionsSoundChannel4)
ON_COMMAND(ID_OPTIONS_SOUND_DIRECTSOUNDA, OnOptionsSoundDirectsounda)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DIRECTSOUNDA, OnUpdateOptionsSoundDirectsounda)
ON_COMMAND(ID_OPTIONS_SOUND_DIRECTSOUNDB, OnOptionsSoundDirectsoundb)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DIRECTSOUNDB, OnUpdateOptionsSoundDirectsoundb)
ON_COMMAND(ID_OPTIONS_GAMEBOY_BORDER, OnOptionsGameboyBorder)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_BORDER, OnUpdateOptionsGameboyBorder)
ON_COMMAND(ID_OPTIONS_GAMEBOY_PRINTER, OnOptionsGameboyPrinter)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_PRINTER, OnUpdateOptionsGameboyPrinter)
ON_COMMAND(ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC, OnOptionsGameboyBorderAutomatic)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC, OnUpdateOptionsGameboyBorderAutomatic)
ON_COMMAND(ID_OPTIONS_GAMEBOY_AUTOMATIC, OnOptionsGameboyAutomatic)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_AUTOMATIC, OnUpdateOptionsGameboyAutomatic)
ON_COMMAND(ID_OPTIONS_GAMEBOY_GBA, OnOptionsGameboyGba)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GBA, OnUpdateOptionsGameboyGba)
ON_COMMAND(ID_OPTIONS_GAMEBOY_CGB, OnOptionsGameboyCgb)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_CGB, OnUpdateOptionsGameboyCgb)
ON_COMMAND(ID_OPTIONS_GAMEBOY_SGB, OnOptionsGameboySgb)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_SGB, OnUpdateOptionsGameboySgb)
ON_COMMAND(ID_OPTIONS_GAMEBOY_SGB2, OnOptionsGameboySgb2)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_SGB2, OnUpdateOptionsGameboySgb2)
ON_COMMAND(ID_OPTIONS_GAMEBOY_GB, OnOptionsGameboyGb)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GB, OnUpdateOptionsGameboyGb)
ON_COMMAND(ID_OPTIONS_GAMEBOY_REALCOLORS, OnOptionsGameboyRealcolors)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_REALCOLORS, OnUpdateOptionsGameboyRealcolors)
ON_COMMAND(ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS, OnOptionsGameboyGameboycolors)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS, OnUpdateOptionsGameboyGameboycolors)
ON_COMMAND(ID_OPTIONS_GAMEBOY_COLORS, OnOptionsGameboyColors)
ON_COMMAND(ID_OPTIONS_FILTER_DISABLEMMX, OnOptionsFilterDisablemmx)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_DISABLEMMX, OnUpdateOptionsFilterDisablemmx)
ON_COMMAND(ID_OPTIONS_LANGUAGE_SYSTEM, OnOptionsLanguageSystem)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_SYSTEM, OnUpdateOptionsLanguageSystem)
ON_COMMAND(ID_OPTIONS_LANGUAGE_ENGLISH, OnOptionsLanguageEnglish)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_ENGLISH, OnUpdateOptionsLanguageEnglish)
ON_COMMAND(ID_OPTIONS_LANGUAGE_OTHER, OnOptionsLanguageOther)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_OTHER, OnUpdateOptionsLanguageOther)
ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_1, OnOptionsJoypadConfigure1)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_1, OnUpdateOptionsJoypadConfigure1)
ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_2, OnOptionsJoypadConfigure2)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_2, OnUpdateOptionsJoypadConfigure2)
ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_3, OnOptionsJoypadConfigure3)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_3, OnUpdateOptionsJoypadConfigure3)
ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_4, OnOptionsJoypadConfigure4)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_4, OnUpdateOptionsJoypadConfigure4)
ON_COMMAND(ID_OPTIONS_JOYPAD_MOTIONCONFIGURE, OnOptionsJoypadMotionconfigure)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_MOTIONCONFIGURE, OnUpdateOptionsJoypadMotionconfigure)
ON_COMMAND(ID_OPTIONS_JOYPAD_ALLOWLEFTRIGHT, OnOptionsJoypadAllowLeftRight)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_ALLOWLEFTRIGHT, OnUpdateOptionsJoypadAllowLeftRight)
ON_COMMAND(ID_OPTIONS_JOYPAD_AUTOFIRE_ACCOUNTFORLAG, OnOptionsJoypadAutofireAccountForLag)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_AUTOFIRE_ACCOUNTFORLAG, OnUpdateOptionsJoypadAutofireAccountForLag)
ON_COMMAND(ID_CHEATS_SEARCHFORCHEATS, OnCheatsSearchforcheats)
ON_UPDATE_COMMAND_UI(ID_CHEATS_SEARCHFORCHEATS, OnUpdateCheatsSearchforcheats)
ON_COMMAND(ID_CHEATS_CHEATLIST, OnCheatsCheatlist)
ON_UPDATE_COMMAND_UI(ID_CHEATS_CHEATLIST, OnUpdateCheatsCheatlist)
ON_COMMAND(ID_CHEATS_AUTOMATICSAVELOADCHEATS, OnCheatsAutomaticsaveloadcheats)
ON_COMMAND(ID_CHEATS_PAUSEDURINGCHEATSEARCH, OnCheatsPauseDuringCheatSearch)
ON_COMMAND(ID_CHEATS_LOADCHEATLIST, OnCheatsLoadcheatlist)
ON_UPDATE_COMMAND_UI(ID_CHEATS_LOADCHEATLIST, OnUpdateCheatsLoadcheatlist)
ON_COMMAND(ID_CHEATS_SAVECHEATLIST, OnCheatsSavecheatlist)
ON_UPDATE_COMMAND_UI(ID_CHEATS_SAVECHEATLIST, OnUpdateCheatsSavecheatlist)
ON_COMMAND(ID_TOOLS_DISASSEMBLE, OnToolsDisassemble)
ON_UPDATE_COMMAND_UI(ID_TOOLS_DISASSEMBLE, OnUpdateToolsDisassemble)
ON_COMMAND(ID_TOOLS_LOGGING, OnToolsLogging)
ON_UPDATE_COMMAND_UI(ID_TOOLS_LOGGING, OnUpdateToolsLogging)
ON_COMMAND(ID_TOOLS_IOVIEWER, OnToolsIoviewer)
ON_UPDATE_COMMAND_UI(ID_TOOLS_IOVIEWER, OnUpdateToolsIoviewer)
ON_COMMAND(ID_TOOLS_MAPVIEW, OnToolsMapview)
ON_UPDATE_COMMAND_UI(ID_TOOLS_MAPVIEW, OnUpdateToolsMapview)
ON_COMMAND(ID_TOOLS_MEMORYVIEWER, OnToolsMemoryviewer)
ON_UPDATE_COMMAND_UI(ID_TOOLS_MEMORYVIEWER, OnUpdateToolsMemoryviewer)
ON_COMMAND(ID_TOOLS_OAMVIEWER, OnToolsOamviewer)
ON_UPDATE_COMMAND_UI(ID_TOOLS_OAMVIEWER, OnUpdateToolsOamviewer)
ON_COMMAND(ID_TOOLS_PALETTEVIEW, OnToolsPaletteview)
ON_UPDATE_COMMAND_UI(ID_TOOLS_PALETTEVIEW, OnUpdateToolsPaletteview)
ON_COMMAND(ID_TOOLS_TILEVIEWER, OnToolsTileviewer)
ON_UPDATE_COMMAND_UI(ID_TOOLS_TILEVIEWER, OnUpdateToolsTileviewer)

ON_COMMAND(ID_DEBUG_NEXTFRAME, OnDebugNextframe)
ON_UPDATE_COMMAND_UI(ID_DEBUG_NEXTFRAME, OnUpdateDebugNextframe)
ON_COMMAND(ID_DEBUG_NEXTFRAME_ACCOUNTFORLAG, OnDebugNextframeAccountForLag)
ON_UPDATE_COMMAND_UI(ID_DEBUG_NEXTFRAME_ACCOUNTFORLAG, OnUpdateDebugNextframeAccountForLag)
ON_COMMAND(ID_DEBUG_FRAMESEARCH, OnDebugFramesearch)
ON_UPDATE_COMMAND_UI(ID_DEBUG_FRAMESEARCH, OnUpdateDebugFramesearch)
ON_COMMAND(ID_DEBUG_FRAMESEARCHPREV, OnDebugFramesearchPrev)
ON_UPDATE_COMMAND_UI(ID_DEBUG_FRAMESEARCHPREV, OnUpdateDebugFramesearchPrev)
ON_COMMAND(ID_DEBUG_FRAMESEARCHLOAD, OnDebugFramesearchLoad)
ON_UPDATE_COMMAND_UI(ID_DEBUG_FRAMESEARCHLOAD, OnUpdateDebugFramesearchLoad)
ON_UPDATE_COMMAND_UI(ID_CHEATS_AUTOMATICSAVELOADCHEATS, OnUpdateCheatsAutomaticsaveloadcheats)
ON_UPDATE_COMMAND_UI(ID_CHEATS_PAUSEDURINGCHEATSEARCH, OnUpdateCheatsPauseDuringCheatSearch)
ON_COMMAND(ID_TOOLS_FRAMECOUNTER, OnToolsFrameCounter)
ON_UPDATE_COMMAND_UI(ID_TOOLS_FRAMECOUNTER, OnUpdateToolsFrameCounter)
ON_COMMAND(ID_TOOLS_LAGCOUNTER, OnToolsLagCounter)
ON_UPDATE_COMMAND_UI(ID_TOOLS_LAGCOUNTER, OnUpdateToolsLagCounter)
ON_COMMAND(ID_TOOLS_EXTRACOUNTER, OnToolsExtraCounter)
ON_UPDATE_COMMAND_UI(ID_TOOLS_EXTRACOUNTER, OnUpdateToolsExtraCounter)
ON_COMMAND(ID_TOOLS_EXTRACOUNTERRESET, OnToolsExtraCounterReset)
ON_COMMAND(ID_TOOLS_INPUTDISPLAY, OnToolsInputDisplay)
ON_UPDATE_COMMAND_UI(ID_TOOLS_INPUTDISPLAY, OnUpdateToolsInputDisplay)
ON_COMMAND(ID_TOOLS_DEBUG_GDB, OnToolsDebugGdb)
ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_GDB, OnUpdateToolsDebugGdb)
ON_COMMAND(ID_TOOLS_DEBUG_LOADANDWAIT, OnToolsDebugLoadandwait)
ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_LOADANDWAIT, OnUpdateToolsDebugLoadandwait)
ON_COMMAND(ID_TOOLS_DEBUG_BREAK, OnToolsDebugBreak)
ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_BREAK, OnUpdateToolsDebugBreak)
ON_COMMAND(ID_TOOLS_DEBUG_DISCONNECT, OnToolsDebugDisconnect)
ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_DISCONNECT, OnUpdateToolsDebugDisconnect)

ON_COMMAND(ID_TOOLS_SOUNDRECORDING, OnToolsSoundRecording)
ON_UPDATE_COMMAND_UI(ID_TOOLS_SOUNDRECORDING, OnUpdateToolsSoundRecording)
ON_COMMAND(ID_TOOLS_AVIRECORDING, OnToolsAVIRecording)
ON_UPDATE_COMMAND_UI(ID_TOOLS_AVIRECORDING, OnUpdateToolsAVIRecording)
ON_COMMAND(ID_TOOLS_PAUSEAVIRECORDING, OnToolsPauseAVIRecording)
ON_UPDATE_COMMAND_UI(ID_TOOLS_PAUSEAVIRECORDING, OnUpdateToolsPauseAVIRecording)

ON_COMMAND(ID_MOVIE_RECORD, OnToolsRecordMovie)
ON_UPDATE_COMMAND_UI(ID_MOVIE_RECORD, OnUpdateToolsRecordMovie)
ON_COMMAND(ID_MOVIE_STOP, OnToolsStopMovie)
ON_UPDATE_COMMAND_UI(ID_MOVIE_STOP, OnUpdateToolsStopMovie)
ON_COMMAND(ID_MOVIE_PLAY, OnToolsPlayMovie)
ON_UPDATE_COMMAND_UI(ID_MOVIE_PLAY, OnUpdateToolsPlayMovie)
ON_COMMAND(ID_MOVIE_READONLY, OnToolsPlayReadOnly)
ON_UPDATE_COMMAND_UI(ID_MOVIE_READONLY, OnUpdateToolsPlayReadOnly)
ON_COMMAND(ID_MOVIE_ASSC_WITH_SAVESTATE, OnAsscWithSaveState)
ON_UPDATE_COMMAND_UI(ID_MOVIE_ASSC_WITH_SAVESTATE, OnUpdateAsscWithSaveState)
ON_COMMAND(ID_MOVIE_RESUME_RECORD, OnToolsResumeRecord)
ON_UPDATE_COMMAND_UI(ID_MOVIE_RESUME_RECORD, OnUpdateToolsResumeRecord)
ON_COMMAND(ID_MOVIE_RESTART_PLAY, OnToolsPlayRestart)
ON_UPDATE_COMMAND_UI(ID_MOVIE_RESTART_PLAY, OnUpdateToolsPlayRestart)

ON_COMMAND(ID_MOVIE_TOOL_EDIT_MODE_PREV, OnToolsEditModePrev)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EDIT_MODE_PREV, OnUpdateToolsEditModePrev)
ON_COMMAND(ID_MOVIE_TOOL_EDIT_MODE_NEXT, OnToolsEditModeNext)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EDIT_MODE_NEXT, OnUpdateToolsEditModeNext)
ON_COMMAND(ID_MOVIE_TOOL_EDIT_MODE_DISCARD, OnToolsEditModeDiscard)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EDIT_MODE_DISCARD, OnUpdateToolsEditModeDiscard)
ON_COMMAND(ID_MOVIE_TOOL_EDIT_MODE_OVERWRITE, OnToolsEditModeOverwrite)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EDIT_MODE_OVERWRITE, OnUpdateToolsEditModeOverwrite)
ON_COMMAND(ID_MOVIE_TOOL_EDIT_MODE_INSERT, OnToolsEditModeInsert)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EDIT_MODE_INSERT, OnUpdateToolsEditModeInsert)
ON_COMMAND(ID_MOVIE_TOOL_EDIT_MODE_XOR, OnToolsEditModeXor)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EDIT_MODE_XOR, OnUpdateToolsEditModeXor)

ON_COMMAND(ID_MOVIE_END_PAUSE, OnToolsOnMovieEndPause)
ON_UPDATE_COMMAND_UI(ID_MOVIE_END_PAUSE, OnUpdateToolsOnMovieEndPause)
ON_COMMAND(ID_MOVIE_END_STOP, OnToolsOnMovieEndStop)
ON_UPDATE_COMMAND_UI(ID_MOVIE_END_STOP, OnUpdateToolsOnMovieEndStop)
ON_COMMAND(ID_MOVIE_END_RESTART, OnToolsOnMovieEndRestart)
ON_UPDATE_COMMAND_UI(ID_MOVIE_END_RESTART, OnUpdateToolsOnMovieEndRestart)
ON_COMMAND(ID_MOVIE_END_APPEND, OnToolsOnMovieEndAppend)
ON_UPDATE_COMMAND_UI(ID_MOVIE_END_APPEND, OnUpdateToolsOnMovieEndAppend)
ON_COMMAND(ID_MOVIE_END_KEEP, OnToolsOnMovieEndKeep)
ON_UPDATE_COMMAND_UI(ID_MOVIE_END_KEEP, OnUpdateToolsOnMovieEndKeep)

ON_COMMAND(ID_MOVIE_TOOL_SET_PAUSE_AT, OnToolsMovieSetPauseAt)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_SET_PAUSE_AT, OnUpdateToolsSetMoviePauseAt)
ON_COMMAND(ID_MOVIE_TOOL_CONVERT, OnToolsMovieConvertCurrent)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_CONVERT, OnUpdateToolsMovieConvertCurrent)
ON_COMMAND(ID_MOVIE_TOOL_AUTO_CONVERT, OnToolsMovieAutoConvert)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_AUTO_CONVERT, OnUpdateToolsMovieAutoConvert)
ON_COMMAND(ID_MOVIE_TOOL_FIX_HEADER, OnToolsMovieFixHeader)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_FIX_HEADER, OnUpdateToolsMovieFixHeader)
ON_COMMAND(ID_MOVIE_TOOL_INSERT_ONE_FRAME, OnToolsMovieInsertOneFrame)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_INSERT_ONE_FRAME, OnUpdateToolsMovieInsertOneFrame)
ON_COMMAND(ID_MOVIE_TOOL_DELETE_ONE_FRAME, OnToolsMovieDeleteOneFrame)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_DELETE_ONE_FRAME, OnUpdateToolsMovieDeleteOneFrame)
ON_COMMAND(ID_MOVIE_TOOL_INSERT_FRAMES, OnToolsMovieInsertFrames)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_INSERT_FRAMES, OnUpdateToolsMovieInsertFrames)
ON_COMMAND(ID_MOVIE_TOOL_DELETE_FRAMES, OnToolsMovieDeleteFrames)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_DELETE_FRAMES, OnUpdateToolsMovieDeleteFrames)
ON_COMMAND(ID_MOVIE_TOOL_TRUNCATE, OnToolsMovieTruncateAtCurrent)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_TRUNCATE, OnUpdateToolsMovieTruncateAtCurrent)
ON_COMMAND(ID_MOVIE_TOOL_EXTRACT_FROM_SAVEGAME, OnToolsMovieExtractFromSavegame)
ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EXTRACT_FROM_SAVEGAME, OnUpdateToolsMovieExtractFromSavegame)

ON_COMMAND(ID_TOOLS_REWIND, OnToolsRewind)
ON_UPDATE_COMMAND_UI(ID_TOOLS_REWIND, OnUpdateToolsRewind)
ON_COMMAND(ID_TOOLS_CUSTOMIZE, OnToolsCustomize)
ON_UPDATE_COMMAND_UI(ID_TOOLS_CUSTOMIZE, OnUpdateToolsCustomize)
// ON_COMMAND(ID_TOOLS_CUSTOMIZE_COMMON, OnToolsCustomizeCommon)
// ON_UPDATE_COMMAND_UI(ID_TOOLS_CUSTOMIZE_COMMON, OnUpdateToolsCustomizeCommon)
ON_COMMAND(ID_HELP_BUGREPORT, OnHelpBugreport)

ON_COMMAND_EX_RANGE(ID_FILE_MRU_FILE1, ID_FILE_MRU_FILE10, OnFileRecentFile)
ON_COMMAND_EX_RANGE(ID_FILE_LOADGAME_SLOT1, ID_FILE_LOADGAME_SLOT10, OnFileLoadSlot)
ON_COMMAND_EX_RANGE(ID_FILE_SAVEGAME_SLOT1, ID_FILE_SAVEGAME_SLOT10, OnFileSaveSlot)
ON_COMMAND_EX_RANGE(ID_SELECT_SLOT1, ID_SELECT_SLOT10, OnSelectSlot)
ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_MRU_FILE1, ID_FILE_MRU_FILE10, OnUpdateFileRecentFile)
ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_LOADGAME_SLOT1, ID_FILE_LOADGAME_SLOT10, OnUpdateFileLoadSlot)
ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_SAVEGAME_SLOT1, ID_FILE_SAVEGAME_SLOT10, OnUpdateFileSaveSlot)
ON_UPDATE_COMMAND_UI_RANGE(ID_SELECT_SLOT1, ID_SELECT_SLOT10, OnUpdateSelectSlot)

ON_COMMAND(ID_FILE_SAVEGAME_OLDESTSLOT, OnFileSavegameOldestslot)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_OLDESTSLOT, OnUpdateFileSavegameOldestslot)
ON_COMMAND(ID_FILE_LOADGAME_MOSTRECENT, OnFileLoadgameMostrecent)
ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_MOSTRECENT, OnUpdateFileLoadgameMostrecent)
ON_COMMAND(ID_FILE_SAVEGAME_CURRENT, OnFileSavegameCurrent)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_CURRENT, OnUpdateFileSavegameCurrent)
ON_COMMAND(ID_FILE_LOADGAME_CURRENT, OnFileLoadgameCurrent)
ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_CURRENT, OnUpdateFileLoadgameCurrent)
ON_COMMAND(ID_FILE_LOADGAME_MAKECURRENT, OnFileLoadgameMakeCurrent)
ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_MAKECURRENT, OnUpdateFileLoadgameMakeCurrent)
ON_COMMAND(ID_FILE_SAVEGAME_MAKECURRENT, OnFileSavegameMakeCurrent)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_MAKECURRENT, OnUpdateFileSavegameMakeCurrent)

ON_COMMAND(ID_FILE_SAVEGAME_INCREMENTSLOT, OnFileSavegameIncrementSlot)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_INCREMENTSLOT, OnUpdateFileSavegameIncrementSlot)
ON_COMMAND(ID_FILE_SAVEGAME_DECREMENTSLOT, OnFileSavegameDecrementSlot)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_DECREMENTSLOT, OnUpdateFileSavegameDecrementSlot)
ON_COMMAND(ID_FILE_SLOT_DISPLAYMODIFICATIONTIME, OnFileSlotDisplayModificationTime)
ON_UPDATE_COMMAND_UI(ID_FILE_SLOT_DISPLAYMODIFICATIONTIME, OnUpdateFileSlotDisplayModificationTime)

ON_COMMAND(ID_FILE_LOADGAME_AUTOLOADMOSTRECENT, OnFileLoadgameAutoloadmostrecent)
ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_AUTOLOADMOSTRECENT, OnUpdateFileLoadgameAutoloadmostrecent)
ON_COMMAND(ID_FILE_LOADGAME_MAKERECENT, OnFileLoadgameMakeRecent)
ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_MAKERECENT, OnUpdateFileLoadgameMakeRecent)

ON_COMMAND(ID_OPTIONS_SOUND_VOLUME_25X, OnOptionsSoundVolume25x)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_VOLUME_25X, OnUpdateOptionsSoundVolume25x)
ON_COMMAND(ID_OPTIONS_SOUND_VOLUME_5X, OnOptionsSoundVolume5x)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_VOLUME_5X, OnUpdateOptionsSoundVolume5x)
ON_COMMAND(ID_CHEATS_DISABLECHEATS, OnCheatsDisablecheats)
ON_UPDATE_COMMAND_UI(ID_CHEATS_DISABLECHEATS, OnUpdateCheatsDisablecheats)
ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE, OnOptionsVideoFullscreenmaxscale)

ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_FRAMESKIP_0, ID_OPTIONS_VIDEO_FRAMESKIP_9, OnOptionsFrameskip)
ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_X1, ID_OPTIONS_VIDEO_X4, OnOptionVideoSize)
ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_LAYERS_BG0, ID_OPTIONS_VIDEO_LAYERS_OBJWIN, OnVideoLayer)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_VIDEO_LAYERS_BG0, ID_OPTIONS_VIDEO_LAYERS_OBJWIN, OnUpdateVideoLayer)
ON_COMMAND(ID_SYSTEM_MINIMIZE, OnSystemMinimize)
ON_COMMAND(ID_SYSTEM_MAXIMIZE, OnSystemMaximize)
ON_COMMAND_EX_RANGE(ID_OPTIONS_EMULATOR_SHOWSPEED_NONE, ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT, OnOptionsEmulatorShowSpeed)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_EMULATOR_SHOWSPEED_NONE,
                           ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT,
                           OnUpdateOptionsEmulatorShowSpeed)
ON_COMMAND_EX_RANGE(ID_OPTIONS_SOUND_VOLUME_1X, ID_OPTIONS_SOUND_VOLUME_4X, OnOptionsSoundVolume)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_SOUND_VOLUME_1X, ID_OPTIONS_SOUND_VOLUME_4X, OnUpdateOptionsSoundVolume)
ON_COMMAND_EX_RANGE(ID_OPTIONS_PRIORITY_HIGHEST, ID_OPTIONS_PRIORITY_BELOWNORMAL, OnOptionsPriority)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_PRIORITY_HIGHEST, ID_OPTIONS_PRIORITY_BELOWNORMAL, OnUpdateOptionsPriority)

ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_NORMAL, ID_OPTIONS_FILTER_TVMODE, OnOptionsFilter)
ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL, ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL, OnOptionsFilter)
ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X, ID_OPTIONS_FILTER16BIT_SIMPLE2X, OnOptionsFilter)
ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_BILINEAR, ID_OPTIONS_FILTER_BILINEARPLUS, OnOptionsFilter)
ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_SCANLINES, ID_OPTIONS_FILTER_SCANLINES, OnOptionsFilter)
ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_LQ2X, ID_OPTIONS_FILTER_HQ3X2, OnOptionsFilter)
ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER16BIT_SIMPLE3X, ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X, OnOptionsFilter)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_NORMAL, ID_OPTIONS_FILTER_TVMODE, OnUpdateOptionsFilter)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL, ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL, OnUpdateOptionsFilter)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X, ID_OPTIONS_FILTER16BIT_SIMPLE2X, OnUpdateOptionsFilter)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_BILINEAR, ID_OPTIONS_FILTER_BILINEARPLUS, OnUpdateOptionsFilter)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_SCANLINES, ID_OPTIONS_FILTER_SCANLINES, OnUpdateOptionsFilter)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_LQ2X, ID_OPTIONS_FILTER_HQ3X2, OnUpdateOptionsFilter)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER16BIT_SIMPLE3X, ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X, OnUpdateOptionsFilter)
ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE, ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART, OnOptionsFilterIFB)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE,
                           ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART,
                           OnUpdateOptionsFilterIFB)

ON_COMMAND_EX_RANGE(ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1, ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4, OnOptionsJoypadDefault)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1, ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4, OnUpdateOptionsJoypadDefault)
ON_COMMAND_EX_RANGE(ID_OPTIONS_JOYPAD_AUTOFIRE_A, ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR, OnOptionsJoypadAutofire)
ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_JOYPAD_AUTOFIRE_A, ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR, OnUpdateOptionsJoypadAutofire)
ON_COMMAND_EX_RANGE(ID_STICKY_A, ID_STICKY_CLEAR, OnOptionsJoypadSticky)
ON_UPDATE_COMMAND_UI_RANGE(ID_STICKY_A, ID_STICKY_CLEAR, OnUpdateOptionsJoypadSticky)
ON_MESSAGE(VBA_CONFIRM_MODE, OnConfirmMode)
ON_MESSAGE(WM_SYSCOMMAND, OnMySysCommand)
ON_COMMAND(ID_OPTIONS_VIDEO_TEXTDISPLAYOPTIONS, OnOptionsVideoTextdisplayoptions)
ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_TEXTDISPLAYOPTIONS, OnUpdateOptionsVideoTextdisplayoptions)

ON_COMMAND(ID_FILE_LUA_OPEN, OnFileLuaOpen)
ON_UPDATE_COMMAND_UI(ID_FILE_LUA_OPEN, OnUpdateFileLuaOpen)
ON_COMMAND(ID_FILE_LUA_CLOSE_ALL, OnFileLuaCloseAll)
ON_UPDATE_COMMAND_UI(ID_FILE_LUA_CLOSE_ALL, OnUpdateFileLuaCloseAll)
ON_COMMAND(ID_FILE_LUA_RELOAD, OnFileLuaReload)
ON_COMMAND(ID_FILE_LUA_STOP, OnFileLuaStop)
ON_COMMAND(ID_RAM_SEARCH, OnFileRamSearch)
ON_UPDATE_COMMAND_UI(ID_RAM_SEARCH, OnUpdateFileRamSearch)
ON_COMMAND(ID_RAM_WATCH, OnFileRamWatch)
ON_UPDATE_COMMAND_UI(ID_RAM_WATCH, OnUpdateFileRamWatch)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MainWnd message handlers

bool vbaShuttingDown = false;

void MainWnd::OnClose()
{
	vbaShuttingDown = true; // HACK to fix crash on exit while memory viewer is open

	CWnd::OnClose();

	delete this;
}

void MainWnd::OnMove(int x, int y)
{
	CWnd::OnMove(x, y);

	if (!theApp.changingVideoSize)
	{
		if (this)
		{
			if (!IsIconic() && !IsZoomed())
			{
				RECT r;

				GetWindowRect(&r);
				theApp.windowPositionX = r.left;
				theApp.windowPositionY = r.top;
				theApp.adjustDestRect();
				regSetDwordValue("windowX", theApp.windowPositionX);
				regSetDwordValue("windowY", theApp.windowPositionY);
			}
		}
	}
}

void MainWnd::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	static int lastType = -1;

	// FIXME: hack to re-maximize window after it auto-unmaximizes while loading a ROM
	if (nType == SIZE_MAXIMIZED && lastType == SIZE_MAXIMIZED)
	{
		lastType = -1;
		ShowWindow(SW_SHOWMAXIMIZED);
		MoveWindow(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
		return;
	}

	lastType = nType;

	if (IsIconic())
	{
		theApp.iconic = true;
		return;
	}
	else if (theApp.iconic)
	{
		theApp.iconic = false;
	}

	if (!theApp.changingVideoSize)
	{
		if (this)
		{
			if (theApp.videoOption <= VIDEO_4X)
			{
				theApp.surfaceSizeX = cx;
				theApp.surfaceSizeY = cy;
				theApp.adjustDestRect();
				if (theApp.display)
					theApp.display->resize(theApp.dest.right - theApp.dest.left, theApp.dest.bottom - theApp.dest.top);
				systemRefreshScreen();  // useful when shrinking
			}
		}
	}
}

void MainWnd::OnContextMenu(CWnd *pWnd, CPoint point)
{
	winMouseOn();
}

void MainWnd::OnSystemMinimize()
{
	ShowWindow(SW_SHOWMINIMIZED);
}

void MainWnd::OnSystemMaximize()
{
	ShowWindow(SW_SHOWMAXIMIZED);
}

void MainWnd::OnPaint()
{
	CPaintDC dc(this); // device context for painting, calling BeginPaint/EndPaint internally
	if (emulating && (!theApp.active || theApp.paused))
	{
		systemRedrawScreen();
	}
}

static bool translatingAccelerator = false;

// FIXME: this fix for accel keys is ugly
//   using too many static variables for a single accel key kludge
static bool	  recursiveCall	  = true;
static bool	  fullUpdated	  = false;
static bool	  lastKeyModifier = false;  // maybe better check current key press status instead
static WPARAM lastKey		  = 0;

BOOL MainWnd::PreTranslateMessage(MSG *pMsg)
{
	if (RamSearchHWnd && ::IsDialogMessage(RamSearchHWnd, pMsg))
	{
		return TRUE;
	}
	else if (RamWatchHWnd && ::IsDialogMessage(RamWatchHWnd, pMsg))
	{
		if (RamWatchAccels)
			TranslateAccelerator(RamWatchHWnd, RamWatchAccels, pMsg);
		return TRUE;
	}
	else if (LuaConsoleHWnd && ::IsDialogMessage(LuaConsoleHWnd, pMsg))
	{
		return TRUE;
	}
	else if (CWnd::PreTranslateMessage(pMsg))
	{
		return TRUE;
	}
	else if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN)
	{
		translatingAccelerator = true;

		bool bHit		= theApp.hAccel != NULL &&  ::TranslateAccelerator(m_hWnd, theApp.hAccel, pMsg);
		bool isModifier = pMsg->wParam == VK_SHIFT || pMsg->wParam == VK_CONTROL || pMsg->wParam == VK_MENU;

		// HACK to get around the fact that TranslateAccelerator can't handle modifier-only accelerators
		// (it would be better to fix TranslateAccelerator, but its code is in a Microsoft library...)
		if (!bHit)
		{
			if (isModifier)
			{
				// do a linear loop through all accelerators to find modifier-only ones...
				CCmdAccelOb *pCmdAccel;
				WORD		 wKey;
				CAccelsOb *	 pAccelOb;
				POSITION	 pos	   = theApp.winAccelMgr.m_mapAccelTable.GetStartPosition();
				const int	 modifiers = ((pMsg->wParam == VK_SHIFT) ? FSHIFT : ((pMsg->wParam == VK_CONTROL) ? FCONTROL : FALT));
				while (pos != NULL)
				{
					theApp.winAccelMgr.m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel);
					POSITION pos = pCmdAccel->m_Accels.GetHeadPosition();
					while (pos != NULL)
					{
						pAccelOb = pCmdAccel->m_Accels.GetNext(pos);

						if (pAccelOb->m_wKey == 0) // if accelerator-only
						{
							if ((pAccelOb->m_cVirt & modifiers) == modifiers) // if modifier matches
							{
								bHit = true;
								SendMessage(WM_COMMAND, pCmdAccel->m_wIDCommand, 0); // tell Windows to call the right function
							}
						}
					}
				}
			}

			if (!bHit)
			{
				lastKeyModifier = true;
			}
		}

		if (bHit)
		{
			if (lastKeyModifier && !isModifier)
			{
				fullUpdated		= false;
				lastKeyModifier = false;
			}

			if (lastKey != pMsg->wParam)
			{
				fullUpdated = false;
				lastKey		= pMsg->wParam;
			}
		}

		translatingAccelerator = false;
		return bHit ? TRUE : FALSE;
	}

	return FALSE;
}

void MainWnd::OnMouseMove(UINT nFlags, CPoint point)
{
	winMouseOn();

	CWnd::OnMouseMove(nFlags, point);
}

// recursive kludge
static void InitMenuKludge(CMenu *pParentMenu, CMenu *pMenu, CCmdTarget *pWnd)
{
	ASSERT(pMenu != NULL);

	CCmdUI state;
	state.m_pParentMenu = pParentMenu;
	state.m_pMenu		= pMenu;
	ASSERT(state.m_pOther == NULL);

	state.m_nIndexMax = pMenu->GetMenuItemCount();
	for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
	     state.m_nIndex++)
	{
		state.m_nID = pMenu->GetMenuItemID(state.m_nIndex);
		if (state.m_nID == 0)
			continue;  // menu separator or invalid cmd - ignore it

		ASSERT(state.m_pOther == NULL);
		ASSERT(state.m_pMenu != NULL);
		if (state.m_nID == (UINT)-1)
		{
			// possibly a popup menu, route to first item of that popup
			state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex);
			if (state.m_pSubMenu == NULL)
			{
				continue; // first item of popup can't be routed to
			}

			state.DoUpdate(pWnd, false);
			if (recursiveCall)
			{
				// FIXME: slow recursive calls to fix enabling/disabling of accel keys
				InitMenuKludge(state.m_pMenu, state.m_pSubMenu, pWnd);
			}
		}
		else
		{
			// normal menu item
			// Auto enable/disable if frame window has 'm_bAutoMenuEnable'
			//    set and command is _not_ a system command.
			state.m_pSubMenu = NULL;
			state.DoUpdate(pWnd, state.m_nID < 0xF000);
		}

		// adjust for menu deletions and additions
		UINT nCount = pMenu->GetMenuItemCount();
		if (nCount < state.m_nIndexMax)
		{
			state.m_nIndex -= (state.m_nIndexMax - nCount);
			while (state.m_nIndex < nCount &&
			       pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
			{
				state.m_nIndex++;
			}
		}
		state.m_nIndexMax = nCount;
	}
}

void MainWnd::OnInitMenuPopup(CMenu *pMenu, UINT nIndex, BOOL bSysMenu)
{
	ASSERT(pMenu != NULL);

	CCmdUI state;
	state.m_pMenu = pMenu;
	ASSERT(state.m_pOther == NULL);
	ASSERT(state.m_pParentMenu == NULL);

	// determine if menu is popup in top-level menu and set m_pOther to
	//  it if so (m_pParentMenu == NULL indicates that it is secondary popup)
	HMENU hParentMenu;
	if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu)
		state.m_pParentMenu = pMenu;  // parent == child for tracking popup
	else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
	{
		CWnd *pParent = GetTopLevelParent();
		// children windows don't have menus -- need to go to the top!
		if (pParent != NULL &&
		    (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
		{
			int nIndexMax = ::GetMenuItemCount(hParentMenu);
			for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
			{
				if (::GetSubMenu(hParentMenu, nIndex) == pMenu->m_hMenu)
				{
					// when popup is found, m_pParentMenu is containing menu
					state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
					break;
				}
			}
		}
	}

	// FIXME: magic to workaround the accel key bug without slowing down too much
	if (translatingAccelerator && !fullUpdated && state.m_pParentMenu == &theApp.m_menu)
	{
		state.m_pMenu = state.m_pParentMenu;
		recursiveCall = true;
		fullUpdated	  = true;
	}
	else if (!translatingAccelerator && fullUpdated)
	{
		fullUpdated = false;
	}

	InitMenuKludge(state.m_pParentMenu, state.m_pMenu, this);

	recursiveCall = false;
}

void MainWnd::OnInitMenu(CMenu *pMenu)
{
//	CWnd::OnInitMenu(pMenu);

	if (translatingAccelerator)
	{}
	else
	{
		// HACK: we only want to call this if the user is pulling down the menu,
		// but TranslateAccelerator also causes OnInitMenu to be called, so ignore that

		systemSoundPause();
	}
}

void MainWnd::OnActivate(UINT nState, CWnd *pWndOther, BOOL bMinimized)
{
	CWnd::OnActivate(nState, pWndOther, bMinimized);

	bool activated = (nState == WA_ACTIVE || nState == WA_CLICKACTIVE) && !bMinimized;

	theApp.active = activated || !theApp.pauseWhenInactive;

	if (theApp.muteWhenInactive)
	{
		theApp.winMuteForNow = !activated;
	}

	extern bool inputActive;
	inputActive = activated || (!theApp.pauseWhenInactive && theApp.enableBackgroundInput);

	if (theApp.active)
	{
		if (theApp.input)
		{
			theApp.input->activate();
		}
	}
	else
	{
		theApp.wasPaused = true;

		memset(theApp.delta, 255, sizeof(theApp.delta));
	}

	if (emulating)
	{
		if (theApp.paused)
		{
			systemRefreshScreen();
		}
		else if (theApp.active)
		{
			systemSoundResume();
		}
		else
		{
			systemSoundPause();
		}
	}
}

#if _MSC_VER <= 1200
void MainWnd::OnActivateApp(BOOL bActive, HTASK hTask)
#else
void MainWnd::OnActivateApp(BOOL bActive, DWORD hTask)
#endif
{
	CWnd::OnActivateApp(bActive, hTask);

	if (theApp.tripleBuffering && theApp.videoOption > VIDEO_4X)
	{
		if (bActive)
		{
			if (theApp.display)
				theApp.display->clear();
		}
	}
}

LRESULT MainWnd::OnMySysCommand(WPARAM wParam, LPARAM lParam)
{
	if (emulating && !theApp.paused)
	{
		if ((wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER)
			return 0;
	}
	return Default();
}

void MainWnd::OnDropFiles(HDROP hDropInfo)
{
	// FIXME: required for the accel key fix
	fullUpdated = false;

	systemSoundClearBuffer();

	char szFile[1024];
	char ext[1024];

	if (DragQueryFile(hDropInfo, 0, szFile, 1024))
	{
		DragFinish(hDropInfo);

		_splitpath(szFile, NULL, NULL, NULL, ext);
		if (strcasecmp(ext, ".lua") == 0)
		{
			if (!emulating)
			{
				theApp.winCheckFullscreen();

				// FIXME: should give the user choices
				int cartType = 0;
				if (winFileOpenSelect(cartType))
				{
					if (!winFileRun())
						return;
				}
				else
					return;
			}

			if (VBALoadLuaCode(szFile))
			{
				// success, there is nothing to do
			}
			else
			{
				// Errors are displayed by the Lua code.
			}
		}
		else if (strcasecmp(ext, ".vbm") == 0)
		{
			SMovie movieInfo;
			char * movieName = szFile;
			char   romTitle [12];
			uint32 romGameCode;
			uint16 checksum;
			uint8  crc;

			if (VBAMovieGetInfo(movieName, &movieInfo) != MOVIE_SUCCESS)
			{
				return;
			}

			int cartType = movieInfo.header.typeFlags & 1 ? 0 : 1;
			if (!emulating)
			{
				theApp.winCheckFullscreen();

				if (winFileOpenSelect(cartType))
				{
					if (!winFileRun())
						return;
				}
				else
					return;
			}
			VBAMovieGetRomInfo(movieInfo, romTitle, romGameCode, checksum, crc);

			while (movieInfo.header.romCRC != crc
			       || strncmp(movieInfo.header.romTitle, romTitle, 12) != 0
			       || movieInfo.header.romOrBiosChecksum != checksum
			       && !((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) == 0 && checksum == 0))
			{
				char msg[1024], warning1[1024], warning2[1024], buffer[1024];

				strcpy(warning1, "");
				strcpy(warning2, "");
				{
					char str [13];
					strncpy(str, movieInfo.header.romTitle, 12);
					str[12] = '\0';
					sprintf(buffer, "title=%s  ", str);
					strcat(warning1, buffer);

					strncpy(str, romTitle, 12);
					str[12] = '\0';
					sprintf(buffer, "title=%s  ", str);
					strcat(warning2, buffer);
				}
				{
					sprintf(buffer, "type=%s  ",
					        (movieInfo.header.typeFlags & MOVIE_TYPE_GBA) ?
					        "GBA" : (movieInfo.header.typeFlags & MOVIE_TYPE_GBC) ?
					        "GBC" : (movieInfo.header.typeFlags & MOVIE_TYPE_SGB) ? "SGB" : "GB");
					strcat(warning1, buffer);

					sprintf(buffer, "type=%s  ", systemCartridgeType ==
					        0 ? "GBA" : (gbRom[0x143] & 0x80 ? "GBC" : (gbRom[0x146] == 0x03 ? "SGB" : "GB")));
					strcat(warning2, buffer);
				}
				{
					sprintf(buffer, "crc=%02x  ", movieInfo.header.romCRC);
					strcat(warning1, buffer);

					sprintf(buffer, "crc=%02x  ", crc);
					strcat(warning2, buffer);
				}
				{
					char code [5];
					if (movieInfo.header.typeFlags & MOVIE_TYPE_GBA)
					{
						memcpy(code, &movieInfo.header.romGameCode, 4);
						code[4] = '\0';
						sprintf(buffer, "code=%s  ", code);
						strcat(warning1, buffer);
					}

					if (systemCartridgeType == 0)
					{
						memcpy(code, &romGameCode, 4);
						code[4] = '\0';
						sprintf(buffer, "code=%s  ", code);
						strcat(warning2, buffer);
					}
				}
				{
					sprintf(buffer,
					        movieInfo.header.typeFlags &
					        MOVIE_TYPE_GBA ? ((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) ==
					                          0 ? "(bios=none)  " : "(bios=%04x)  ") : "check=%04x  ",
					        movieInfo.header.romOrBiosChecksum);
					strcat(warning1, buffer);

					sprintf(buffer,
					        checksum == 0 ? "(bios=none)  " : systemCartridgeType == 0 ? "(bios=%04x)  " : "check=%04x  ",
					        checksum);
					strcat(warning2, buffer);
				}

				strcpy(msg, "");
				sprintf(buffer, "Movie ROM: %s\n", warning1);
				strcat(msg, buffer);
				sprintf(buffer, "Your ROM: %s\n", warning2);
				strcat(msg, buffer);
				strcat(msg, "still want to play the movie?");

				int sel = MessageBox(msg, TEXT("ROM Mismatch"), MB_ABORTRETRYIGNORE | MB_ICONQUESTION);
				switch (sel)
				{
				case IDABORT:
					return;
				case IDRETRY:
					theApp.winCheckFullscreen();
					if (winFileOpenSelect(cartType))
					{
						if (VBAMovieIsActive())
							VBAMovieStop(false);  // will only get here on user selecting to play a ROM, canceling movie
						if (!winFileRun())
							return;
						VBAMovieGetRomInfo(movieInfo, romTitle, romGameCode, checksum, crc);
					}
					else
						return;
					break;
				default:
					goto romcheck_exit;
				}
			}
romcheck_exit:

			bool useBIOSFile = (movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0;
			if (useBIOSFile)
			{
				if (!systemLoadBIOS(theApp.biosFileName, useBIOSFile))
				{
					systemMessage(0, "This movie requires a valid GBA BIOS file to play.\nPlease locate a BIOS file.");
					((MainWnd *)theApp.m_pMainWnd)->OnOptionsEmulatorSelectbiosfile();
					if (!systemLoadBIOS(theApp.biosFileName, useBIOSFile))
					{
						systemMessage(0, "\"%s\" is not a valid BIOS file; cannot play movie without one.", theApp.biosFileName);
						return;
					}
				}
			}

			int code = VBAMovieOpen(movieName, TRUE);

			if (code != MOVIE_SUCCESS)
			{
				if (code == MOVIE_FILE_NOT_FOUND)
					systemMessage(0, "Could not find movie file \"%s\".", (const char *)movieName);
				else if (code == MOVIE_WRONG_FORMAT)
					systemMessage(0, "Movie file \"%s\" is not in proper VBM format.", (const char *)movieName);
				else if (code == MOVIE_WRONG_VERSION)
					systemMessage(0, "Movie file \"%s\" is not a supported version.", (const char *)movieName);
				else
					systemMessage(0, "Failed to open movie \"%s\".", (const char *)movieName);
				return;
			}
		}
		else if (strcasecmp(ext, ".wch") == 0)
		{
			if (emulating)
			{
				MainWnd::OnFileRamWatch();
				Load_Watches(true, szFile);
			}
		}
		else
		{
			theApp.romFilename = szFile;
			if (winFileRun())
			{
				SetForegroundWindow();
			}
		}
	}
	else
		DragFinish(hDropInfo);
}

/////////////////////

void MainWnd::winMouseOn()
{
	SetCursor(arrow);
	if (theApp.videoOption > VIDEO_4X)
	{
		theApp.mouseCounter = 120;
	}
	else
		theApp.mouseCounter = 0;
}

void MainWnd::winConfirmMode()
{
	if (theApp.renderMethod == DIRECT_DRAW && theApp.videoOption > VIDEO_4X)
	{
		theApp.winCheckFullscreen();
		ModeConfirm dlg(this);

		if (!dlg.DoModal())
		{
			theApp.updateVideoSize(ID_OPTIONS_VIDEO_X2);
		}
	}
}

bool MainWnd::winFileOpenSelect(int cartridgeType)
{
	int selectedFilter = regQueryDwordValue("selectedFilter", 0);
	if (selectedFilter < 0 || selectedFilter > 2)
		selectedFilter = 0;

	LPCTSTR exts[] = { NULL };
	CString filter = winResLoadFilter(IDS_FILTER_ROM);
	CString title  = winResLoadString(IDS_SELECT_ROM);

	bool	isOverrideEmpty = false;
	CString initialDir		= regQueryStringValue(cartridgeType == 0 ? IDS_ROM_DIR : IDS_GBXROM_DIR, NULL);
	if (initialDir.IsEmpty())
	{
		isOverrideEmpty = true;
		CString altDir = regQueryStringValue(cartridgeType != 0 ? IDS_ROM_DIR : IDS_GBXROM_DIR, NULL);
		initialDir = altDir.IsEmpty() ? theApp.exeDir : altDir;
	}

	FileDlg dlg(this, "", filter, selectedFilter, "ROM", exts, initialDir, title, false, true);

	if (dlg.DoModal() == IDOK)
	{
		regSetDwordValue("selectedFilter", dlg.m_ofn.nFilterIndex);
		theApp.romFilename = dlg.GetPathName();
		initialDir		   = winGetDirFromFilename(theApp.romFilename);

		// we have directory override for that purpose
		// but this can be...desirable
		if (isOverrideEmpty)
			regSetStringValue(cartridgeType == 0 ? IDS_ROM_DIR : IDS_GBXROM_DIR, initialDir);
		return true;
	}
	return false;
}

// some extensions that might commonly be near emulation-related files that we almost certainly can't open, or at least not
// directly.
// also includes definitely non-ROM extensions we know about, since we only use this variable in a ROM opening function.
// we do this by exclusion instead of inclusion because we don't want to exclude extensions used for any archive files, even
// extensionless or unusually-named archives.
static const char *s_romIgnoreExtensions[] = {
	"vbm", "sgm",  "clt",  "dat",	"gbs",	"gcf",	"spc", "xpc", "pal", "act", "dmp", "avi", "ini", "txt", "nfo",
	"htm", "html", "jpg",  "jpeg",	"png",	"bmp",	"gif", "mp3", "wav", "lnk", "exe", "bat", "sav", "luasav"
};

#include "Dialogs/GBACheatsDlg.h"
#include "Dialogs/GBCheatsDlg.h"

#include "../common/CheatSearch.h"
#include "../gba/GBA.h"
#include "../gb/GB.h"
#include "../gba/Flash.h"
#include "../gba/RTC.h"

void MainWnd::winFileClose(bool reopening)
{
	if (rom != NULL || gbRom != NULL)
	{
		if (theApp.autoSaveLoadCheatList)
			winSaveCheatListDefault();

		if (!reopening)
		{
			// save battery file before we change the filename...
			winWriteBatteryFile();
			cheatSearchCleanup(&cheatSearchData);
			capturePrevious = false;
			captureNumber	= 0;
		}

		theApp.emulator.emuCleanUp();

		extern void remoteCleanUp();
		remoteCleanUp();
	}

	if (VBAMovieIsActive())
		VBAMovieStop(false);  // will only get here on user selecting to open a ROM, canceling movie

	theApp.frameSearching	   = false;
	theApp.frameSearchSkipping = false;
	emulating = 0;

	if (this)
		RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN);
	systemSetTitle(VBA_NAME_AND_VERSION);
}

bool MainWnd::winFileRun(bool reopening)
{
	int prevCartridgeType = systemCartridgeType;

	//bool requiresInitRAMSearch = (rom == NULL && gbRom == NULL) || !reopening;
	winFileClose(reopening);

	// use ObtainFile to support opening files within archives (.7z, .rar, .zip, .zip.rar.7z, etc.)
	if (theApp.romFilename.GetLength() > 2048) theApp.romFilename.Truncate(2048);

	char logicalName[2048], physicalName[2048];

	// FIXME: assertion failure in fopen.c if canceled
	if (ObtainFile(theApp.romFilename, logicalName, physicalName, "rom", s_romIgnoreExtensions,
	               sizeof(s_romIgnoreExtensions) / sizeof(*s_romIgnoreExtensions)))
	{
		// theApp.romFilename is exactly the filename used for opening, while theApp.gameFilename is always the logical name
		theApp.romFilename = theApp.gameFilename = logicalName;
		ReleaseTempFileCategory("rom", physicalName);
	}
	else
	{
		return false;
	}

	IMAGE_TYPE type = utilFindType(physicalName);

	if (type == IMAGE_UNKNOWN)
	{
		systemMessage(IDS_UNSUPPORTED_FILE_TYPE,
		              "The file \"%s\" is an unsupported type.", logicalName);
		return false;
	}
	systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
	systemCartridgeType		= (int)type;
	if (type == IMAGE_GB)
	{
		if (!gbLoadRom(physicalName))
			return false;

		gbBorderOn		= theApp.winGbBorderOn;
		theApp.emulator = GBSystem;
		theApp.romSize	= gbRomSize;
		if (theApp.autoIPS)
		{
			CString ipsname = winGetDestFilename(logicalName, IDS_IPS_DIR, ".ips");
			int		size	= gbRomSize;
			utilApplyIPS(ipsname, &gbRom, &size);
			if (size != gbRomSize)
			{
				extern bool gbUpdateSizes();
				gbUpdateSizes();
				theApp.romSize = size;
			}
		}

		useBios = false;    // FIXME
		
		if (reopening)
		{
			bool winGbCheatReaddress();
			winGbCheatReaddress();
		}
	}
	else
	{
		int size = CPULoadRom(physicalName);
		if (!size)
			return false;

		flashSetSize(theApp.winFlashSize);
		rtcEnable(theApp.winRtcEnable);
		cpuSaveType = theApp.winSaveType;

		//    if(cpuEnhancedDetection && winSaveType == 0) {
		//      utilGBAFindSave(rom, size);
		//    }

		char buffer[5];
		strncpy(buffer, (const char *)&rom[0xac], 4);
		buffer[4] = 0;

		// vba-over.ini
		CString vbaOverINI = theApp.exeDir;
		vbaOverINI += "\\vba-over.ini";

		UINT i = GetPrivateProfileInt(buffer, "rtcEnabled", -1, vbaOverINI);
		if (i != (UINT)-1)
			rtcEnable(i == 0 ? false : true);

		i = GetPrivateProfileInt(buffer, "flashSize", -1, vbaOverINI);
		if (i != (UINT)-1)
			if (i == 0x10000 || i == 0x20000)
				flashSetSize((int32)i);

		i = GetPrivateProfileInt(buffer, "saveType", -1, vbaOverINI);
		if (i != (UINT)-1 && (i <= 5))
			cpuSaveType = (int32)i;

#ifndef USE_GBA_CORE_V7
		i = GetPrivateProfileInt(buffer, "mirroringEnabled", -1, vbaOverINI);
		if (i != (UINT)-1)
			CPUDoMirroring(i != 0);
#endif

		/* disabled due to problems
		   if(theApp.removeIntros && rom != NULL) {
		   *((u32 *)rom)= 0xea00002e;
		   }
		 */
		theApp.emulator = GBASystem;
		theApp.romSize	= size;
		if (theApp.autoIPS)
		{
			CString ipsname = winGetDestFilename(logicalName, IDS_IPS_DIR, ".ips");
			int		size	= 0x2000000;
			utilApplyIPS(ipsname, &rom, &size);
		}

		skipBios = theApp.skipBiosFile ? true : false;
		CPUInit();
		systemLoadBIOS(theApp.biosFileName, theApp.useBiosFile ? true : false);
		CPUReset();

		if (reopening)
		{
			bool winGbaCheatReaddress();
			winGbaCheatReaddress();
		}
	}

	if (!soundOffFlag)
		systemSoundCleanInit();

	winReadBatteryFile();

	emulating = true;

	if (theApp.autoSaveLoadCheatList)
		winLoadCheatListDefault();

	if (theApp.filenamePreference == 0)
		theApp.addRecentFile(winGetOriginalFilename(logicalName));
	else
		theApp.addRecentFile(logicalName);

	theApp.updateWindowSize(theApp.videoOption);

	theApp.updateFrameSkip();

	if (theApp.autoHideMenu && theApp.videoOption > VIDEO_4X && theApp.menuToggle)
		OnFileTogglemenu();

	if (theApp.autoLoadMostRecent && !VBAMovieIsActive() && !VBAMovieIsLoading()) // would cause desync in movies...
		OnFileLoadgameMostrecent();

	theApp.renderedFrames = 0;

	theApp.rewindCount		= 0;
	theApp.rewindCounter	= 0;
	theApp.rewindSaveNeeded = false;

	{
		extern bool playMovieFile, playMovieFileReadOnly, outputWavFile, outputAVIFile, flagHideMenu; // from VBA.cpp
		extern char movieFileToPlay[1024], wavFileToOutput[1024]; // from VBA.cpp
		extern int	pauseAfterTime; // from VBA.cpp
		if (playMovieFile)
		{
			playMovieFile = false;
			VBAMovieOpen(movieFileToPlay, playMovieFileReadOnly);
		}
		if (outputWavFile)
		{
			outputWavFile = false;
			theApp.soundRecordName = wavFileToOutput;
			theApp.soundRecording  = true;
		}
		if (outputAVIFile)
		{
			outputAVIFile = false;
			OnToolsStartAVIRecording();
		}
		if (pauseAfterTime >= 0)
		{
			VBAMovieSetPauseAt(pauseAfterTime);
		}
		if (flagHideMenu)
		{
			OnFileTogglemenu();
			theApp.updateWindowSize(theApp.videoOption);
		}
	}

	if (systemCartridgeType != prevCartridgeType)
	{
		extern GBACheatSearch gbaDlg;
		extern GBCheatSearch  gbDlg;
		if (!theApp.pauseDuringCheatSearch && theApp.modelessCheatDialogIsOpen)
		{
			gbaDlg.DestroyWindow();
			gbDlg.DestroyWindow();
			theApp.modelessCheatDialogIsOpen = false;
		}
	}

	theApp.winCheckFullscreen();
	ReopenRamWindows();

	// FIXME
	reset_address_info();
	//if (requiresInitRAMSearch)
	{
		// extern void soft_reset_address_info();
		// soft_reset_address_info();
	}

	VBAUpdateButtonPressDisplay();
	VBAUpdateFrameCountDisplay();
	systemRefreshScreen();

	return true;
}

